package com.example.libo.huihui.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.libo.huihui.R;
import com.example.libo.huihui.been.QingdanEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by libo on 2017/1/9.
 */

public class QingdanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private ArrayList datas;
    private IloadMore iloadMore;

    public QingdanAdapter(Context context, ArrayList datas){
        this.context = context;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == R.layout.item_qingdan) {
            view = LayoutInflater.from(context).inflate(R.layout.item_qingdan, parent, false);
            return new QingdanViewHolder(view);
        } else{
            view = LayoutInflater.from(context).inflate(R.layout.recycler_bottom, parent, false);
            return new LoadViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof QingdanViewHolder){
            QingdanViewHolder qingdanHolder = (QingdanViewHolder) holder;
            QingdanEntity.DataBean entity = (QingdanEntity.DataBean) datas.get(position);
            Picasso.with(context).load(entity.getImage_url()).into(qingdanHolder.ivCover);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            qingdanHolder.recyclerView.setLayoutManager(linearLayoutManager);
            HorizonImageAdapter adapter = new HorizonImageAdapter((ArrayList) entity.getItems(),context);
            qingdanHolder.recyclerView.setAdapter(adapter);
        }else{
            iloadMore.loadMore();
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(position == datas.size()){
            return R.layout.recycler_bottom;
        }else{
            return R.layout.item_qingdan;
        }

    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

    public static class QingdanViewHolder extends RecyclerView.ViewHolder{
        private ImageView ivCover;
        private RecyclerView recyclerView;

        public QingdanViewHolder(View itemView) {
            super(itemView);
            ivCover = (ImageView) itemView.findViewById(R.id.iv_pic);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.rv_horizontal);
        }
    }

    public static class LoadViewHolder extends RecyclerView.ViewHolder{

        public LoadViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface IloadMore{
        void loadMore();
    }

    public void setLoadMore(IloadMore loadMore){
        this.iloadMore = loadMore;
    }

}
